package com.guoguo.cloudmusic.ui.common

import android.graphics.Color
import android.os.Bundle
import android.os.PersistableBundle
import android.util.Log
import android.view.View
import android.widget.Toolbar
import androidx.annotation.ColorRes
import androidx.appcompat.app.AppCompatActivity
import com.guoguo.cloudmusic.R
import com.gyf.immersionbar.ImmersionBar
import com.xuexiang.xui.utils.StatusBarUtils

/** 应用所有Activity的基类
 * @author GuoGuo
 * @date 2021/12/15
 */
abstract class BaseActivity : AppCompatActivity() ,View.OnClickListener{
    override fun onClick(p0: View?) {
        val toolbar:Toolbar = findViewById(R.id.toolbar)
        if(p0 == toolbar){
            toolbar.setNavigationOnClickListener { finish() }
        }
    }
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        ActivityCollector.addActivity(this)
        Log.d("BaseActivity", javaClass.simpleName)
        initBinding()
        initData()
        initView()
        initBroadcastReceiver()
        initListener()
        initObserver()
        initBar()
    }

    protected open fun initBinding() {}
    protected open fun initView() {}
    protected open fun initData() {}
    protected open fun initListener() {}
    protected open fun initObserver() {}
    protected open fun initBroadcastReceiver() {}
    protected open fun initBar() {
        StatusBarUtils.translucent(this, Color.TRANSPARENT)
        StatusBarUtils.setStatusBarLightMode(this);
    }

    /**
     * 设置状态栏背景色
     */
    open fun setStatusBarBackground(@ColorRes statusBarColor: Int) {
        ImmersionBar.with(this).autoStatusBarDarkModeEnable(true, 0.2f)
            .statusBarColor(statusBarColor).fitsSystemWindows(true).init()
    }

    override fun onDestroy() {
        super.onDestroy()
        ActivityCollector.removeActivity(this)
    }
}